<template>
    <view class="bg-content">
        <u-sticky>
            <div style="height: 49px; border-bottom: 1rpx solid #eeeeee; background-color: #FFFFFF;">
                <u-tabs :list="tabList" :gutter="20" active-color="red" :current="activeTabIndex" @change="onTabChange"></u-tabs>
            </div>
        </u-sticky>

        <goodsList ref="goodsListRef" v-if="tabList.length && tabList[activeTabIndex].type !== '1'" :goodsListFilterParams="goodsListFilterParams"></goodsList>

        <tbStoreList ref="tbStoreListRef" :tbStoreListFilterParams="tbStoreListFilterParams"
                     v-if="tabList.length && tabList[activeTabIndex].type === '1'"></tbStoreList>
    </view>
</template>

<script>

    import goodsList from '@/components/goods-list.vue'
    import tbStoreList from '@/components/store-list.vue'
    import {TbStoreClassService} from "@/dev/services/tb-store-class.service";

    export default {
        data() {
            return {
                parentStoreClass: null,
                tbStoreClass: null,
                tabList: [],
                activeTabIndex: 0,
                goodsListFilterParams: {
                    communityId: this.globalVariable.userInfo.tbCommunity.id,
                    searchKeyword: null,
                    storeClassId: null,
                    orderBy: 'salesvolume desc'
                },
                tbStoreListFilterParams: {
                    communityId: this.globalVariable.userInfo.tbCommunity.id,
                    searchKeyword: null,
                    storeClassId: null,
                    orderBy: 'coordinate desc'
                }
            }
        },
        components: {
            goodsList,
            tbStoreList,
        },
        onReady() {
            const _self = this;
            _self.parentStoreClass = _self.objParams.parentStoreClass;
            _self.tbStoreClass = _self.objParams.tbStoreClass;

            uni.setNavigationBarTitle({
                title: _self.parentStoreClass.name
            });

            _self.goodsListFilterParams.parentStoreClassId = _self.parentStoreClass.id;
            _self.goodsListFilterParams.storeClassId = _self.tbStoreClass.id;
            _self.tbStoreListFilterParams.parentStoreClassId = _self.parentStoreClass.id;
            _self.tbStoreListFilterParams.storeClassId = _self.tbStoreClass.id;

            new TbStoreClassService().findTbStoreClassList({
                communityId: _self.globalVariable.userInfo.tbCommunity.id,
                parentId: _self.parentStoreClass.id,
                orderBy: 'display_order asc'
            }).then(result => {
                _self.tabList = [{id: null, name: '全部', type: _self.tbStoreClass.type}, ...result];
                _self.tabList.forEach((item, index) => {
                    if (_self.tbStoreClass.id === item.id) {
                        _self.activeTabIndex = index;
                    }
                });
            });
        },
        methods: {
            onTabChange(index) {
                const _self = this;
                this.activeTabIndex = index;
                _self.goodsListFilterParams.storeClassId = _self.tabList[_self.activeTabIndex].id;
                _self.tbStoreListFilterParams.storeClassId = _self.tabList[_self.activeTabIndex].id;
            }
        },
        // 上拉加载
        onReachBottom: function () {
            if (this.tabList[this.activeTabIndex].type !== '1') {  // 商品列表
                if (this.$refs.goodsListRef.pagination.hasNextPage) {
                    this.$refs.goodsListRef.doInfinite();
                }
            } else if (this.tabList[this.activeTabIndex].type === '1') {  // 店铺列表
                if (this.$refs.tbStoreListRef.pagination.hasNextPage) {
                    this.$refs.tbStoreListRef.doInfinite();
                }
            }
        },
    }
</script>

<style lang="scss" scoped>
</style>
